/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.datamodel;

import br.jm.financeiro.entity.Proposta;
import br.jm.financeiro.filter.Funcoes;
import br.jm.financeiro.filter.UploadArquivo;
import br.jm.financeiro.sistema.eao.PropostaEao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author joao
 */
public class LazyProposta extends LazyDataModel<Proposta> {

    private static final long serialVersionUID = -8904794691684695127L;

    private List<Proposta> datasource;
    private final PropostaEao propostaJpa;
    private final String cFiltro;
    private String cParametroSQL;
    private boolean lSorter;
    private int dataSize;
    private final UploadArquivo arquivo;
    private final Funcoes funcoes;
    private final FacesMessage msg = null;

    public LazyProposta(PropostaEao propostaJpa, String cFiltro) {
        this.propostaJpa = propostaJpa;
        this.dataSize = 0;
        this.cFiltro = cFiltro;
        this.dataSize = propostaJpa.getCount("Proposta");
        arquivo = new UploadArquivo();
        funcoes = new Funcoes();
    }

    @Override
    public Proposta getRowData(String rowKey) {
        for (Proposta proposta : datasource) {
            if (proposta.getDscProposta().equals(rowKey)) {
                return proposta;
            }
        }

        return null;
    }

    @Override
    public Object getRowKey(Proposta proposta) {
        return proposta.getIdProposta();
    }

    @Override
    public void setRowIndex(int rowIndex) {
        /*
         * The following is in ancestor (LazyDataModel):
         * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
         */
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        } else {
            super.setRowIndex(rowIndex % getPageSize());
        }
    }

    @Override
    public List<Proposta> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        String cCaminho = this.arquivo.getRealPath() + "\\documento\\";

        List<Proposta> data = new ArrayList<>();

        try {
            //filter
            lSorter = SortOrder.ASCENDING.equals(sortOrder);

            data = propostaJpa.getNative("Proposta", first, pageSize, sortField, lSorter, filters, cParametroSQL, cFiltro);
            /*
             for (Proposta data1 : data) {
             String cArquivo = data1.getNomArquivo();
             if (cArquivo != null) {
             funcoes.apagarArquivo(cCaminho + cArquivo);
             data1.setNomArquivo(null);
             propostaJpa.edit(data1);
             }
             }
             */
        } catch (Exception ex) {
            Logger.getLogger(LazyProposta.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.setRowCount(dataSize);
        if (filters.isEmpty()) {
            if (data.isEmpty()) {
                this.setRowCount(0);
            } else {
                this.setRowCount(this.dataSize);
            }
        }
        if (this.msg != null) {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        return data;
    }

}
